var user = document.querySelector('.name');
    var span = user.nextElementSibling;
    var pass = document.querySelector('.pwd1');
    var span2 = pass.nextElementSibling;
    var pass2 = document.querySelector('.pwd2');
    var span3 = pass2.nextElementSibling;
    var pnum = document.querySelector('.phone');
    var span4 = pnum.nextElementSibling;
    var btn = document.querySelector('.btn');
    //用户名
    var useReg = /^[\u4E00-\u9FA5\w\-]{4,20}$/;
    user.addEventListener('blur',function(){
        useReg.test(this.value)&&(span.innerHTML='用户名正确') && (a =true);
        !useReg.test(this.value)&&(span.innerHTML='用户名格式不对') && (a = false);
    });
    //密码
    var passReg = /.{6,20}/;
     pass.addEventListener('blur',function(){
         if (passReg.test(this.value)) {
             var a =0; b=0; c=0;
             var reg = /\d+/;
             var reg1 = /[a-zA-Z]+/;
             var reg2 = /[^a-zA-Z0-9]+/;
             a = reg.test(this.value)? 1:0;
             b = reg1.test(this.value)? 1:0;
             c = reg2.test(this.value)? 1:0;

             var str = '';
             switch(a+b+c){
                 case 1 : 
                 str = '弱';
                 break;
                 case 2:
                     str = '中';
                     break;
                     case 3: 
                     str = '强';
                     break;
             }
             span2.innerHTML = str;
             b= true;
         }else{
             span2.innerHTML = '格式不正确';
             b= false;
         }
     });
     //重复密码
    pass2.addEventListener('blur',function(){
        if (this.value == pass.value) {
            span3.innerHTML = '密码一致';
            span2.innerHTML = '密码一致';
            c= true;
        }else{
            span3.innerHTML = '密码不一致';
            span2.innerHTML = '密码不一致';
            c=false;
        }
    });

    //手机号
    var pnumReg = /^1{1}[35789]{1}\d{9}$/;
    pnum.addEventListener('blur',function(){
        pnumReg.test(this.value)&&(span4.innerHTML = '手机号正确')&&(d = true);
        !pnumReg.test(this.value)&&(span4.innerHTML = '格式不正确')&&(d=true);

    });
   

//    btn.addEventListener('click',function(){
//        if(a&&b&&c&&d){
//            console.log('提交成功');
//        }else{
//            console.log('提交失败');
//        }
//    })

var times = "";
$(".btn").click(function () {
 clearTimeout(times)
 if(a&&b&&c&&d){
    console.log('提交成功');
    $.ajax({
        url: "http://localhost:1000/api",
        data: {
          type: "register",
          username: $(".name").val(),
          password: $(".pwd1").val(),
          phone: $(".phone").val(),
        },
        success: (res) => {
          console.log(res);
          if (JSON.parse(res).code == 1) {
              if(confirm('注册成功,是否跳转')){

                  times = setTimeout(() => {
                    window.location.href = "./login.html";
                  }, 1500);
              }
          }else{
              alert('用户名重复');
          }
        },
      });

}else{
    console.log('提交失败');
}
  
});